/*
 * @Author: xiaosihan
 * @Date: 2024-09-12 22:27:42
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-18 01:37:38
 */

import projectCollaborationApi from "@api/projectCollaborationApi";
import { ProjectCollaborationData } from "@type";
import { get } from "lodash";
import { observable, configure } from "mobx";
configure({ enforceActions: "never" });

// 项目管理
const projectCollaborationStore = observable(
  {
    page: 1, //当前页面
    pageSize: 12, //分页数
    shared: false, // 是否查询参与的项目
    status: 0 as 0 | 1, //，0为未完成，1为已完成
    loading: true,
    setStatus(status: 0 | 1) {
      this.status = status;
      this.getList();
    },
    total: 0, //总数
    list: [] as Array<ProjectCollaborationData>, //项目列表
    //请求项目
    async getList(page: number = 1, pageSize: number = 12) {
      this.page = page;
      this.pageSize = pageSize;
      this.loading = true;
      const res = await projectCollaborationApi.get(page, pageSize, {
        shared: this.shared,
        status: this.status,
      });

      const data = get(res, ["data", "data"]);
      this.list = data; // 项目列表数据
      this.total = parseInt(get(res, ["data", "totalCount"]));
      this.loading = false;
    },
    //新增项目中
    isAdd: false,
    setIsAdd(isAdd: boolean) {
      this.isAdd = isAdd;
    },
    //新增项目
    async add(name: string, description: string) {
      this.status = 0;
      this.loading = true;
      await projectCollaborationApi.add(name, description);
      // 新增完后请求项目
      this.getList();
    },
    //删除项目
    async delete(id: string) {
      this.loading = true;
      await projectCollaborationApi.delete(id);
      // 删除完后请求项目
      this.getList();
    },
    //更新项目
    async updata(projectCollaborationData: Partial<ProjectCollaborationData>) {
      this.loading = true;
      await projectCollaborationApi.update(projectCollaborationData);
      // 更新完后请求项目
      this.getList();
    },
    //退出项目
    async exit(id: string) {
      await projectCollaborationApi.exit(id);
      this.getList();
    },
  },
  {},
  { deep: false }
);
export default projectCollaborationStore;
